package main

import (
     "fmt"
     "log"
     "context"
     "go.mongodb.org/mongo-driver/bson"    //BOSN解析包
     "go.mongodb.org/mongo-driver/mongo"    //MongoDB的Go驱动包
     "go.mongodb.org/mongo-driver/mongo/options"
)

type People struct {
       Name string `json:"Name" bson:"Name"`
       Age  int `json:"Age" bson:"Age"`
}

func main() {
	clientDB := connectDB()
	collection := createCollection(clientDB)
	findDB(collection)
	deleteMany(collection)
	findDB(collection)
	disconnectDB(clientDB)
}

func connectDB() *mongo.Client {
     // Set client options
     clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:27017")
     // Connect to MongoDB, client 就是与 mongodb 的连接对象, 代表整个 mongo 数据库
     client, err := mongo.Connect(context.TODO(), clientOptions)
     if err != nil {
         log.Fatal(err)
     }   
     // Check the connection
     err = client.Ping(context.TODO(), nil)
     if err != nil {
         log.Fatal(err)
     }   
     fmt.Println("===== Connected to MongoDB!")
     return client
}

func disconnectDB(clientDB *mongo.Client) {
     err := clientDB.Disconnect(context.TODO())
     if err != nil {
         log.Fatal(err)
     }
     fmt.Println("===== Connection to MongoDB closed.")
 }

func createCollection(clientDB *mongo.Client) *mongo.Collection {
     // mongo 中可以有很多数据库 database，每个 database有很多 collection
     // collection 是要操作的数据集(相当于表), 
     // 此处指向的数据库是 test, 数据表是 col1
     collection := clientDB.Database("orcafs").Collection("col1")
     fmt.Println("===== create DB orcafs and table col1")
     return collection
}


// 没有找到也不会报错，因此要判断查询结果的个数
func findDB(collection *mongo.Collection) {
    // Passing bson.D{{}} as the filter matches all documents in the collection 
    record, err := collection.Find(context.TODO(), bson.D{{}})
    if err != nil {
        log.Fatal(err)
    }
    defer func() {
        if err := record.Close(context.TODO()); err != nil {
            log.Fatal(err)
        }
    }()

    allRecord := []People{}
    _ = record.All(context.TODO(), &allRecord) //也可以用 next
    fmt.Println("===== find all db")
    for _, r := range allRecord {
        fmt.Println(r)
    }
}

func deleteMany(collection *mongo.Collection) {
	/*
	filter := bson.D{
		{Key:"Name", Value:"aaa"},
		{Key:"Age", Value:10},
	}
	*/

	/*
	filterMany := bson.D{
		{Key: "$or", Value: bson.A{
			bson.D{{Key: "Name", Value: "bbb"}, {Key: "Age", Value: 10}},
			bson.D{{Key: "Name", Value: "ccc"}, {Key: "Age", Value: 10}},
		}},
	}
	*/

	/*
	stu1 := People{Name: "aaa", Age: 10}
	stu2 := People{Name: "bbb", Age: 10}

	filterMany := make([]interface{}, 2)
	filterMany[0] = stu1
	filterMany[1] = stu2
	*/

	var list bson.A

	del1 := bson.D{{Key: "Name", Value: "bbb"}, {Key: "Age", Value: 10}}
	del2 := bson.D{{Key: "Name", Value: "aaa"}, {Key: "Age", Value: 10}}
	list = append(list, del1)
	list = append(list, del2)

	filterMany := bson.D{{Key: "$or", Value: list}}

	_, err := collection.DeleteMany(context.TODO(), filterMany)
	if err != nil {
        log.Fatal(err)
	}
}
